package com.kaifamiao.context;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet( { "/application/name" , "/app/name" } )
public class ApplicationNameServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request , HttpServletResponse response )
            throws ServletException, IOException {

        ServletContext application = request.getServletContext();

        // 获取当前Web应用的名称
        String name = application.getServletContextName();
        // 获取当前Web应用的路径
        String path = application.getContextPath();

        response.setContentType( "text/html;charset=UTF-8" );

        PrintWriter out = response.getWriter();

        out.println( "<title>当前应用</title>" );
        out.println( "<p align='center'>当前Web应用的名称 " + name + "</p>" );
        out.println( "<p align='center'>当前Web应用的路径 " + path + "</p>" );

    }

}
